<?php
namespace app\manage\controller;
use think\facade\Db;
class User extends Base
{
    //用户列表
    public function userList() {
        $search = input('post.search','');
        $curr_page = input('post.pageIndex',1);
        $perpage = input('post.pageSize',10);

        $where = [
            ['del','=',0]
        ];
        if($search) {
            $where[] = ['tel','like',"%{$search}%"];
        }
        try {
            $count = Db::table('mp_user')->where($where)->count();
            $list = Db::table('mp_user')->where($where)
                ->order(['id'=>'DESC'])
                ->limit(($curr_page - 1)*$perpage,$perpage)
                ->field('id,tel,nickname,realname,avatar,gender,wx_openid,alipay_user_id,status')
                ->select();
        }catch (\Exception $e) {
            return ajax($e->getMessage(),-1);
        }
        $ret = [
            'count' => $count,
            'list' => $list
        ];
        return ajax($ret);
    }

    public function userDetail() {
        $post['id'] = input('post.id','');
        checkPost($post);
        try {
            $where = [
                ['id','=',$post['id']]
            ];
            $userinfo = Db::table('mp_user')->where($where)->find();
            if(!$userinfo) {
                return ajax('id',-4);
            }
            if($userinfo['wx_openid']) {
                $userinfo['wx_info'] = Db::table('mp_user_wechat')->where('openid','=',$userinfo['wx_openid'])->find();
            }
            if($userinfo['alipay_user_id']) {
                $userinfo['ali_info'] = Db::table('mp_user_alipay')->where('user_id','=',$userinfo['alipay_user_id'])->find();
            }
        } catch (\Exception $e) {
            return ajax($e->getMessage(),-1);
        }
        return ajax($userinfo);
    }

    public function userBlock() {
        $post['id'] = input('post.id','');
        checkPost($post);
        try {
            $where = [
                ['id','=',$post['id']]
            ];
            $userinfo = Db::table('mp_user')->where($where)->find();
            if(!$userinfo) {
                return ajax('id',-4);
            }
            if($userinfo['status'] === 1) {
                Db::table('mp_user')->where($where)->update(['status'=>2]);
                Db::table('mp_token')->where('uid','=',$userinfo['id'])->delete();
            }else {
                Db::table('mp_user')->where($where)->update(['status'=>1]);
            }
        } catch (\Exception $e) {
            return ajax($e->getMessage(),-1);
        }
        return ajax();
    }



}